<template>
  <el-dialog
    :title="!dataForm.id ? '新建' : '编辑'"
    :close-on-click-modal="false"
    append-to-body
    :visible.sync="visible"
    class="jsbos-dialog jsbos-dialog_center"
    lock-scroll
    width="600px"
  >
    <el-row :gutter="15" class="">
      <el-form
        ref="elFormTemplate"
        :model="dataForm"
        :rules="rules"
        size="small"
        label-width="100px"
        label-position="right"
      >
        <template v-if="!loading">
          <el-col :span="24">
            <el-form-item label="消息渠道" prop="messageType">
              <el-select
                v-model="dataForm.messageType"
                placeholder="请选择"
                clearable
                :style="{ width: '100%' }"
                @change="onMessageTypeChange"
              >
                <el-option
                  v-for="(item, index) in messageTypeOptions"
                  :key="index"
                  :label="item.fullName"
                  :value="item.enCode"
                  :disabled="item.disabled"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="模板名称" prop="templateId">
              <MsgTemplateDialog
                v-model="dataForm.templateId"
                :title="dataForm.templateName"
                :messageSource="messageSource"
                :messageType="dataForm.messageType"
                @change="onTemplateChange"
                :clearable="false"
              />
              <!-- <el-input
                v-model="dataForm.templateId"
                placeholder="请输入"
                clearable
                :style="{ width: '100%' }"
              >
              </el-input> -->
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="模板编码" prop="templateEnCode">
              <el-input
                disabled
                v-model="dataForm.templateEnCode"
                placeholder="请输入"
                clearable
                :style="{ width: '100%' }"
              >
              </el-input>
            </el-form-item>
          </el-col>
          <el-col
            :span="24"
            v-if="
              ['2', '3', '4', '5', '6', '7', '8'].includes(dataForm.messageType)
            "
          >
            <el-form-item label="应用名称" prop="accountConfigId">
              <MsgAccountDialog
                v-model="dataForm.accountConfigId"
                :title="dataForm.accountName"
                :messageType="dataForm.messageType"
                @change="onAccountChange"
                :clearable="false"
              />
              <!-- <el-input
                v-model="dataForm.accountConfigId"
                placeholder="请输入"
                clearable
                :style="{ width: '100%' }"
              >
              </el-input> -->
            </el-form-item>
          </el-col>
          <el-col
            :span="24"
            v-if="
              ['2', '3', '4', '5', '6', '7', '8'].includes(dataForm.messageType)
            "
          >
            <el-form-item label="应用编码" prop="accountEnCode">
              <el-input
                disabled
                v-model="dataForm.accountEnCode"
                placeholder="请输入"
                clearable
                :style="{ width: '100%' }"
              >
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="排序码" prop="sortCode">
              <el-input-number
                v-model="dataForm.sortCode"
                placeholder="数字文本"
                :step="1"
                controls-position="right"
                :style="{ width: '100%' }"
              >
              </el-input-number>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="状态" prop="enabledMark">
              <el-switch
                v-model="dataForm.enabledMark"
                :active-value="1"
                :inactive-value="0"
              >
              </el-switch>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="描述" prop="description">
              <el-input
                v-model="dataForm.description"
                placeholder="请输入"
                :style="{ width: '100%' }"
                true
                type="textarea"
                :autosize="{ minRows: 2, maxRows: 4 }"
              >
              </el-input>
            </el-form-item>
          </el-col>
        </template>
      </el-form>
    </el-row>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false"> 取 消</el-button>
      <el-button type="primary" @click="dataFormSubmit()"> 确 定</el-button>
    </span>
  </el-dialog>
</template>
<script>
import MsgTemplateDialog from "./msgTemplateDialog";
import MsgAccountDialog from "./msgAccountDialog";
export default {
  components: { MsgTemplateDialog, MsgAccountDialog },
  props: {
    messageSource: {
      type: String,
      default: "",
    },
  },
  data() {
    return {
      visible: false,
      loading: false,
      dataForm: {
        sendConfigId: "",
        messageType: "",
        messageTypeName: "",
        templateId: "",
        templateName: "",
        templateEnCode: "",
        accountConfigId: "",
        accountName: "",
        accountEnCode: "",
        description: "",
        sortCode: 0,
        enabledMark: 1,
      },
      rules: {
        sendConfigId: [
          {
            required: true,
            message: "请输入",
            trigger: "blur",
          },
        ],
        messageType: [
          {
            required: true,
            message: "请选择",
            trigger: "change",
          },
        ],
        templateId: [
          {
            required: true,
            message: "请输入",
            trigger: "blur",
          },
        ],
        accountConfigId: [
          {
            required: true,
            message: "请输入",
            trigger: "blur",
          },
        ],
      },
      messageTypeOptions: [],
      currentIndex: undefined,
    };
  },
  computed: {},
  watch: {},
  created() {
    this.getMessageTypeOptions();
  },
  mounted() {},
  methods: {
    /**消息渠道change事件 */
    onMessageTypeChange(val) {
      this.clearData(this.dataForm);
      this.dataForm.enabledMark = 1;
      this.dataForm.sortCode = 0;
      this.dataForm.messageType = val;
      let obj = this.messageTypeOptions.find((item) => {
        return item.enCode === val;
      });
      this.dataForm.messageTypeName = obj.fullName;
    },
    /**模板名称change事件 */
    onTemplateChange(id, item) {
      if (!id) return (this.dataForm.templateName = "");
      this.dataForm.templateName = item.fullName;
      this.dataForm.templateEnCode = item.enCode;
    },
    /**账号change事件 */
    onAccountChange(id, item) {
      if (!id) return (this.dataForm.accountName = "");
      this.dataForm.accountName = item.fullName;
      this.dataForm.accountEnCode = item.enCode;
    },
    getMessageTypeOptions() {
      this.$store
        .dispatch("base/getDictionaryData", {
          encode: "msgSendType",
        })
        .then((res) => {
          this.messageTypeOptions = res;
        });
    },
    clearData(data) {
      for (let key in data) {
        if (data[key] instanceof Array) {
          data[key] = [];
        } else if (data[key] instanceof Object) {
          this.clearData(data[key]);
        } else {
          data[key] = "";
        }
      }
    },
    init(data, index) {
      this.visible = true;
      this.$nextTick(() => {
        // this.$refs.elFormTemplate.resetFields();
        this.currentIndex = index;
        if (data) {
          this.dataForm = Object.assign({}, this.dataForm, data);
        } else {
          this.clearData(this.dataForm);
          this.dataForm.enabledMark = 1;
          this.dataForm.sortCode = 0;
        }
      });
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs["elFormTemplate"].validate((valid) => {
        if (valid) {
          this.$emit("changeTemplateList", this.dataForm, this.currentIndex);
          this.visible = false;
        }
      });
    },
  },
};
</script>
